<?php 
	class Comment_model extends CI_Model{
		function __contruct(){
			parent::__contruct();
		}
		function get_cmt($id_pro,$page=0,$limit=6){
			$query = $this->db->where("obj_id",$id_pro)->order_by("created","DESC")->get("comment");
			//->limit($limit,$page)
			$count = $query->num_rows();
			
			$sql = $this->db->last_query()." Limit $page,$limit";
			
			$info_cmt = $this->db->query($sql)->result();
			$info_cmt = $this->get_user_cmt($info_cmt);
			
			
			return array("result"=>$info_cmt,"total_row"=>$count);
		}
		function get_user_cmt($info_cmt){
			if($info_cmt){
				$count = 0;
				foreach($info_cmt as $item){
					if($item->table == "customers"){
						  $this->db->select("fullname,id,email,CONCAT('".$item->table."') as obj_table");
					}else{
						//$this->db->select("name,id,email");
						$this->db->select("name as fullname,id,email,CONCAT('".$item->table."') as obj_table");
					}
					$info_cmt[$count++]->info_user = $this->db->where("id",$item->customer_id)->get($item->table)->row();	
				}	
			}
			return $info_cmt;
		}
		function get_many_by($where="",$limit,$page){
			if(!empty($where)){
				$this->db->where($where);
			}
			$total_row = $this->db->select("COUNT(id) as total_row")->get("comment")->row();
			if(!empty($where)){
				$this->db->where($where);
			}
			$result = $this->db->select("comment.*,products.id as id_pro,products.name as name_pro,products.slug as slug_pro")->join("products","products.id = comment.obj_id","left")->order_by("comment.id","DESC")->limit($limit,$page)->get("comment")->result();
			$result = $this->get_user_cmt($result);
			return array("total_row"=>$total_row->total_row,"result"=>$result);
		}
		function get_detail_info($id){
			return $this->db->where("id",$id)->get("comment")->row_array();	
		}
		function get_email_recieve(){
			return $this->db->get("email_receive_news")->result();	
		}
		function insert($insert){
			$this->db->insert("comment",$insert);
		}
		function update($update,$id){
			$this->db->where("id",$id)->update("comment",$update);	
		}
	}
?>